import { Component } from "@angular/core";
import {
    NavController,
    NavParams,
    AlertController,
    ActionSheetController,
    ToastController, LoadingController
} from "ionic-angular";
import { HttpService } from "../../../providers/http.service";
import { CommonFunc } from "../../../providers/commonFunc";
import { Storage } from "@ionic/storage";
import { SafeControl } from "../safe-control/bean/safe-control.bean";
import { appConfig } from "../../../providers/config";
import { PhotoViewer } from "@ionic-native/photo-viewer";
import * as moment from "moment";
import { HomePage } from "../../home/home";
import { ElevatorUsedInspectionPage } from "../elevator-used-inspection/elevator-used-inspection";
import { SecurityManagePage } from "../security-manage/security-manage";
import { RecordCheckDtPage } from "../record-check-detail/record-check-detail";
import { SafeControlBookBean} from "../safe-control/bean/safe-control-book.bean";
import {CameraOptions} from "@ionic-native/camera";
import {FileTransferObject, FileUploadOptions} from "@ionic-native/file-transfer";
import {RecordMedia} from "../record-media/record-media";
import {CheckItem} from "../check-item/check-item";
import {SafeControlDownload} from "../safe-control/safe-control-download/safe-control-download";

/**
 * 单位安全管理检查明细
 */
@Component({
  templateUrl: "safe-control-book.html"
})
export class SafeControlBookPage {
    private safeControl: SafeControl = new SafeControl();
    questions: any[];
    laws: any[];
    measures: any[];
    appConfig = appConfig;
    userId: any;
    userName: any;
    partyId: any;
    partyOtion: any = [];
    isLook = false;
    questionList: any = [];
    lawContentList: any = [];
    adr:string = "";
    year:string = "";
    num:string = "";
    modifyType: string ;
    treatmentDate: string = "";//整改日期
    tempTreatmentMeasures: string = "";
    rows:number ;

    constructor(
        public navCtrl: NavController,
        private alertCtrl: AlertController,
        private httpService: HttpService,
        private navParams: NavParams,
        private commonFunc: CommonFunc,
        private storage: Storage,
        private photoViewer: PhotoViewer,
        private actionSheet: ActionSheetController,
        private toast: ToastController,
        private loadc: LoadingController
    ) {

        this.storage.get("safeControl2").then(data=>{
            if (data != null) {
                this.safeControl.treatmentMeasures = data.treatmentMeasures;
                this.safeControl.reconsiderationUnit = data.reconsiderationUnit;
                this.safeControl.litigationUnit = data.litigationUnit;
                this.safeControl.isPreviewLawDoc = data.isPreviewLawDoc;
                this.treatmentDate =moment (data.year + data.month  + data.day +"").format("YYYY-MM-DD");
                this.tempTreatmentMeasures = data.treatmentMeasures
                let temp = this.safeControl.documentNumber;
                if (temp != null) {
                    this.adr = temp.substr(0, temp.indexOf("市"));
                    this.year = temp.substr(temp.indexOf("[")+1, temp.indexOf("]")-temp.indexOf("[")-1);
                    this.num = temp.substr(temp.indexOf("(")+1, temp.indexOf(")")-temp.indexOf("(")-1);
                }
            }
        });
        this.safeControl = this.navParams.get("safeControl");
        this.lawContentList = this.navParams.get("lawContentList");
        this.questionList = this.navParams.get("questionList");

        this.storage.get("user").then(data => {
            this.userId = data.id;
            this.userName = data.userName;
        });

        this.changeAreaHeight();
    }

    ionViewWillEnter(){
        this.changeAreaHeight();
    }

    ionViewWillLeave() {
        this.safeControl.year = this.treatmentDate.substr(0,4);
        this.safeControl.month = this.treatmentDate.substr(5,2);
        this.safeControl.day = this.treatmentDate.substr(8,2);

        let law = this.adr +"市监特令["+this.year+"] 第("+this.num+")号";
        this.safeControl.documentNumber = law;

        this.storage.set("safeControl2",this.safeControl);
    }

    submit() {
        if (this.safeControl.reconsiderationUnit == null || this.safeControl.reconsiderationUnit == "") {
            let t = this.toast.create({
                message: "请输入行政复议部门",
                duration: 2000,
                position: "top"
            });
            t.present();
            return;
        }
        if (this.safeControl.litigationUnit == null || this.safeControl.litigationUnit == "") {
            let t = this.toast.create({
                message: "请输入行政诉讼部门",
                duration: 2000,
                position: "top"
            });
            t.present();
            return;
        }
        if (this.treatmentDate == "") {
            let t = this.toast.create({
                message: "请选择限期整改时间",
                duration: 2000,
                position: "top"
            });
            t.present();
            return;
        }
        let law = this.adr +"市监特令[ "+this.year+" ] 第( "+this.num+" )号";
        this.safeControl.documentNumber = law;

        this.safeControl.year = this.treatmentDate.substr(0,4);
        this.safeControl.month = this.treatmentDate.substr(5,2);
        this.safeControl.day = this.treatmentDate.substr(8,2);

        let load = this.loadc.create({
            spinner: appConfig.spinner,
            duration: appConfig.timeout
        });
        load.present();

        console.log(this.safeControl);
        this.httpService.addSafeControlRecord(this.safeControl).subscribe(data1 => {
            load.dismiss();
            if (data1.status == 200) {
                window["jQuery"]("#signatureid").remove();
                window["jQuery"]("#showImage").remove();
                //提交成功后清空缓存信息
                this.storage.remove("producePartyResult");
                this.storage.remove("userPartyResult");
                this.storage.remove("elevatorResult");
                this.storage.remove("checkPartyResult");
                this.storage.remove("defineResult");
                this.storage.remove("producePartyStatus");
                this.storage.remove("userPartyStatus");
                this.storage.remove("elevatorStatus");
                this.storage.remove("checkPartyStatus");
                this.storage.remove("definedStatus");
                this.storage.remove("elevatorTotalResult");
                this.storage.remove("safeControl");
                this.storage.remove("safeControl2");

                let tt = this.toast.create({
                    message: "提交成功",
                    duration: 2000,
                    position: "top"
                });
                // tt.present().then(() => {
                //     this.navCtrl.setRoot(HomePage).then(() => {
                //         window["jQuery"]("#signatureid").remove();
                //         window["jQuery"]("#showImage").remove();
                //         this.navCtrl
                //             .getActive()
                //             .getNav()
                //             .remove(0);
                //     });
                // });
                this.navCtrl.push(SafeControlDownload, {data : data1.data});
            } else {
                const alert = this.alertCtrl.create({
                    subTitle: "信息保存失败",
                    buttons: ["确定"]
                });
                alert.present();
            }
        })
    }

    changeMethod(){
        if (this.modifyType == "1") {
            if (this.tempTreatmentMeasures != null && this.tempTreatmentMeasures != "") {
                this.safeControl.treatmentMeasures = this.tempTreatmentMeasures
            }else {
                this.safeControl.treatmentMeasures = this.navParams.get("treatmentMeasures");
            }
        }
        if (this.modifyType == "2") {
            if (this.tempTreatmentMeasures != null && this.tempTreatmentMeasures != "") {
                this.safeControl.treatmentMeasures = this.tempTreatmentMeasures
            }else {
                this.safeControl.treatmentMeasures = "";
            }

        }
    }

    changeAreaHeight(){
        this.rows =Math.ceil((this.safeControl.treatmentMeasures.length / 25)) + 2;
    }

    getImgUrl(param) {
        this.safeControl.checkNameUrl = param.imageDate;
    }
}